﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eShop.Model;
using eShop.Business;
using eShop.Model.MetaData;

public partial class admin_controls_shipping_rate_form : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindFormDataToUI();
            BindModelToUI();
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                SaveShippingRate();
                uccResponseMessage.SetSuccessMessage("Data saved successfully");
                uccResponseMessage.Visible = true;
            }
        }
        catch (Exception exception)
        {
            uccResponseMessage.SetFailMessage("Failed to save data." + "<br />" + exception.Message);
            uccResponseMessage.Visible = true;
        }
        finally
        {
            BindModelToUI();
        }
    }

    private void BindFormDataToUI()
    {
        BindCountry();
        BindStateByCountry(Convert.ToInt32(ddlCountry.SelectedValue));
        BindCityByState(Convert.ToInt32(ddlState.SelectedValue));
    }

    private void BindCountry()
    {
        ddlCountry.DataSource = MetaDataManager.GetAllCountries();
        ddlCountry.DataBind();
        ddlCountry.SelectedIndex = 0;
    }

    private void BindStateByCountry(int countryId)
    {
        ddlState.DataSource = MetaDataManager.GetAllStatesByCountry(countryId);
        ddlState.DataBind();
        ddlState.SelectedIndex = 0;
    }

    private void BindCityByState(int stateId)
    {
        ddlCity.DataSource = MetaDataManager.GetAllCitiesByState(stateId);
        ddlCity.DataBind();
        ddlCity.SelectedIndex = 0;
    }

    private void BindModelToUI()
    {
        if (Id > 0)
        {
            ShippingRate shippingRate = LoadShippingRate();
            BindShippingRateToUI(shippingRate);
        }
        else
        {
            ClearUI();
        }
    }

    private void ClearUI()
    {
        ddlCountry.SelectedIndex = 0;
        ddlState.SelectedIndex = 0;
        ddlCity.SelectedIndex = 0;
        tbWeightFrom.Text = "";
        tbWeightTo.Text = "";
        tbRate.Text = "";
    }

    private void BindShippingRateToUI(ShippingRate shippingRate)
    {
        if (shippingRate.Id > 0)
        {
            City city = MetaDataManager.GetCityById(shippingRate.ToCity.Id);
            State state = MetaDataManager.GetStateById(city.CityState.Id);
            ddlCountry.SelectedValue = state.StateCountry.Id.ToString();
            BindStateByCountry(state.StateCountry.Id);
            ddlState.SelectedValue = city.CityState.Id.ToString();
            BindCityByState(city.CityState.Id);
            ddlCity.SelectedValue = shippingRate.ToCity.Id.ToString();
        }
        tbWeightFrom.Text = shippingRate.FromWeight.ToString();
        tbWeightTo.Text = shippingRate.ToWeight.ToString();
        tbRate.Text = shippingRate.Rate.ToString();
    }

    private ShippingRate LoadShippingRate()
    {
        if (Id > 0)
        {
            return ShippingRateManager.GetShippingRateById(Id);
        }
        else
        {
            return new ShippingRate();
        }
    }

    private void SaveShippingRate()
    {
        ShippingRate shippingRate = LoadShippingRate();

        shippingRate.ToCity.Id = Convert.ToInt32(ddlCity.SelectedValue);
        shippingRate.FromWeight = (float)Convert.ToDouble(tbWeightFrom.Text);
        shippingRate.ToWeight = (float)Convert.ToDouble(tbWeightTo.Text);
        shippingRate.Rate = (float)Convert.ToDouble(tbRate.Text);

        if (shippingRate.Id > 0)
        {
            ShippingRateManager.UpdateShippingRate(shippingRate);
        }
        else
        {
            ShippingRateManager.CreateShippingRate(shippingRate);
        }
    }

    private Int32? _id;
    private Int32 Id
    {
        get
        {
            if (!_id.HasValue)
            {
                try
                {
                    if (HttpContext.Current.Request.QueryString != null && !String.IsNullOrEmpty(HttpContext.Current.Request.QueryString["id"]))
                    {
                        _id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
                    }
                    else
                    {
                        _id = -1;
                    }
                }
                catch
                {
                    _id = -1;
                }
            }
            return _id.Value;
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindStateByCountry(Convert.ToInt32(ddlCountry.SelectedValue));
        BindCityByState(Convert.ToInt32(ddlState.SelectedValue));
    }

    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindCityByState(Convert.ToInt32(ddlState.SelectedValue));
    }
}